package com.testingedu.action.background;

import java.util.Date;

import com.testingedu.jdbcDao.TEmploymentDao;
import com.testingedu.jdbcDao.TUserDao;
import com.testingedu.model.TEmployment;
import com.testingedu.model.TUser;
import com.testingedu.utils.Format;

public class TEmploymentAction extends RequestContext{

	private static final long serialVersionUID = 8167639580634618129L;
	private TEmployment tEmployment;
	private static TEmploymentDao tEmploymentDao = new TEmploymentDao();
	public TEmployment gettEmployment() {
		return tEmployment;
	}
	public void settEmployment(TEmployment tEmployment) {
		this.tEmployment = tEmployment;
	}
	
	public void _add() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			int userId = Integer.parseInt(request.getParameter("userId"));
			TUser tUser = new TUserDao().info(userId);
			String nowTime = Format.formatDateDay(new Date());
			request.setAttribute("tUser", tUser);
			request.setAttribute("nowTime", nowTime);
			request.getRequestDispatcher("/WEB-INF/admin/jsp/employment/add.jsp").forward(
							request, response);
		} else {
			StringBuffer msg = new StringBuffer();
			int userId = Integer.parseInt(request.getParameter("userId"));
			TUser tUser = new TUser(userId);
			tEmployment.settUser(tUser);
			int id = 0;
			String em = request.getParameter("em");
			if("no".equals(em)) {
				id = tEmploymentDao.add(tEmployment, 1);
			} else {
				if("yes".equals(em)) {
					id = tEmploymentDao.add(tEmployment, 2);
				}
			}
			msg.append("{\"id\":" + id + "");
			msg.append(",\"msg\":\"null\"}");
			response.getWriter().write(msg.toString());
			response.getWriter().close();
		}
	}

	public void _delete() throws Exception {
		tEmploymentDao.delete(tEmployment.getId());
		response.sendRedirect("/testingedu2/admin/employment/list");
	}

	public void _update() throws Exception {
		String url = request.getParameter("url");
		if ("update".equals(url)) {
			String nowTime = Format.formatDateDay(new Date());
			request.setAttribute("nowTime", nowTime);
			this.info(url);
		} else {
			StringBuffer msg = new StringBuffer();
			String em = request.getParameter("em");
			tEmploymentDao.update(tEmployment, em);
			msg.append("{\"id\":" + tEmployment.getId() + "");
			msg.append(",\"msg\":\"null\"}");
			response.getWriter().write(msg.toString());
			response.getWriter().close();
		}
	}

	public void _list() throws Exception {
		String url = request.getParameter("url");
		if ("info".equals(url)) {
			this.info(url);
		} else {
			createQueryResult();
			String userIdStr = request.getParameter("userId");
			int userId = 0;
			if(userIdStr != null && userIdStr.trim().length() > 0) {
				userId = Integer.parseInt(userIdStr);
			}
			tEmploymentDao.list(queryResult, tEmployment, userId);
			request.setAttribute("queryResult", queryResult);
			request.setAttribute("userId", userId);
			request.getRequestDispatcher(
					"/WEB-INF/admin/jsp/employment/list.jsp").forward(request,
					response);
		}
	}

	private void info(String url) throws Exception {
		tEmployment = tEmploymentDao.info(tEmployment.getId());
		request.setAttribute("tEmployment", tEmployment);
		request.getRequestDispatcher(
				"/WEB-INF/admin/jsp/employment/" + url + ".jsp").forward(
				request, response);
	}
	
}
